package org.mybatis.generator.plugins;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description: 对生成的Mapper类进行操作
 * @author: 01369542
 * @create: 2018/07/19
 **/
public class MyMapperClassPlugin extends PluginAdapter {
    public boolean validate(List<String> arg0) {
        return true;
    }

    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        //获取实体类
        FullyQualifiedJavaType entityType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());

        //import实体类
        interfaze.addImportedType(entityType);

        FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType("Mapper<" + introspectedTable.getBaseRecordType() + ">");
        FullyQualifiedJavaType imp = new FullyQualifiedJavaType("tk.mybatis.mapper.common.Mapper");
        interfaze.addSuperInterface(fqjt);// 添加 extends BaseDao<User>
        interfaze.addImportedType(imp);// 添加import common.BaseDao;
        interfaze.getMethods().clear();

        //添加Mapper类的注释
        interfaze.addJavaDocLine("/**");
        interfaze.addJavaDocLine(" * @Description " + introspectedTable
                .getFullyQualifiedTable().getRemarks() + " 的Mapper类");
        interfaze.addJavaDocLine(" * @Author 01369542");
        interfaze.addJavaDocLine(" * @Date " + new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        interfaze.addJavaDocLine(" */");

        return true;
    }

    /*
     * @Description: 不在Mapper类中生成Insert方法
     * @Author: 01369542
     * @Date: 2018/7/18
     */
    @Override
    public boolean clientInsertMethodGenerated(Method method, Interface interfaze,
                                               IntrospectedTable introspectedTable) {
        return false;
    }

    /*
     * @Description: 不在Mapper类中生成SelectAll方法
     * @Author: 01369542
     * @Date: 2018/7/18
     */
    @Override
    public boolean clientSelectAllMethodGenerated(Method method,
                                                  Interface interfaze, IntrospectedTable introspectedTable) {
        return false;
    }


}
